﻿using MediatR;
using System.WXT.Domain;
using System.WXT.Domain.DTO;
using System.WXT.Infrstructre.Interface;
using System.WXT.WebAPI.Application.Command.WorkerCommand;

namespace System.WXT.WebAPI.Application.CommandHandlers.WorkerCommandHandlers
{
    public class WorkerShowCommandHandlers:IRequestHandler<WorkerShowCommand,PageDTO<Worker>>
    {
        protected readonly IWorkerRepository workerRepository;

        public WorkerShowCommandHandlers(IWorkerRepository workerRepository)
        {
            this.workerRepository = workerRepository;
        }
        /// <summary>
        /// 工人显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<PageDTO<Worker>> Handle(WorkerShowCommand request, CancellationToken cancellationToken)
        {
            PageDTO<Worker> page = new PageDTO<Worker>();
            var list = (await workerRepository.GetAll()).Where(x => x.workerIsDel == 0);
            if(!string.IsNullOrEmpty(request.workerNumbering))
            {
                list = list.Where(x => x.workerNumbering == request.workerNumbering);
            }
            if (!string.IsNullOrEmpty(request.workerName))
            {
                list = list.Where(x => x.workerName == request.workerName);
            }
            if(!string.IsNullOrEmpty(request.workerState))
            {
                list = list.Where(x => x.workerState == request.workerState);
            }
            page.listcount = list.Count();
            page.pagecount = (int)Math.Ceiling(list.Count() * 1.0 / request.pageSize);
            page.plist = list.OrderByDescending(x=>x.workerId).Skip((request.pageIndex-1)*request.pageSize).Take(request.pageSize).ToList();
            return page;
        }
    }
}
